package com.AUI.components
{
	import com.AUI.define.style.*;
	
	import flash.events.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	/**
	 * Input.
	 * @author HuangMinPing
	 * @site www.AminLab.com
	 */
	public class TextArea extends ScrollBase
	{
		/**
		 * [element] text.
		 */
		public var e_text:TextField = new TextField;
		/**
		 * Last height.
		 */
		private var _lastHeight:int = 18;
		/**
		 * Constructor.
		 */
		public function TextArea(text:String = '')
		{
			// init attributes
			depth = 3;
			// init elements
			e_text.type = TextFieldType.INPUT;
			e_text.autoSize = TextFieldAutoSize.LEFT;
			e_text.multiline = true;
			e_text.wordWrap  = true;
			e_text.text = text;
			e_text.width  = 100;
			_lastHeight = e_text.height;
			elements.addChild(e_text);
			// init events
			e_text.addEventListener(Event.CHANGE,onChange,false,0,true);
			
			/**************** INITCOMPLETE ****************/
			Object(this).constructor == TextArea && initComplete();
		}
		/**
		 * @private
		 */
		override protected function initComplete():void
		{
			super.initComplete();
			verticalScrollBar = new VScrollBar;
		}
		/**
		 * Get or set the text of the label.
		 */
		override public function set value(variable:*):void
		{
			var label:String = variable as String;
			e_text.text = label;
			updateContentBox();
		}
		override public function get value():*
		{
			return e_text.text;
		}
		/**
		 * Get or set readonly
		 */
		public function set readonly(value:Boolean):void
		{
			e_text.type = value ? TextFieldType.DYNAMIC : TextFieldType.INPUT;
		}
		public function get readonly():Boolean
		{
			return e_text.type == TextFieldType.DYNAMIC;
		}
		/**
		 * Length.
		 */
		public function get length():int
		{
			return e_text.length;
		}
		/**
		 * Max length.
		 */
		public function set maxLength(value:int):void
		{
			e_text.maxChars = value;
		}
		public function get maxLength():int
		{
			return e_text.maxChars;
		}
		/**
		 * Append text.
		 */
		public function appendText(text:String):void
		{
			e_text.appendText(text);
			onChange(null);
		}
		/**
		 * @private
		 */
		override protected function updateDrawStyle():void
		{
			super.updateDrawStyle();
			// update text
			var font:FONT = drawStyle.font; 
			var textFormat:TextFormat = new TextFormat(font.family,font.size,font.color,font.bold,font.italic,font.underline,null,null,font.align);
			e_text.defaultTextFormat = textFormat;
			e_text.setTextFormat(textFormat);
		}
		/**
		 * @private
		 */
		override protected function resizeBoxModel():void
		{
			if(isAutoWidth() == true)
			{
				drawStyle.content.width.type  = DIM_TYPE.AUTO;
				drawStyle.content.width.value = 100;
			}
			// super do
			super.resizeBoxModel();
			e_text.width = boxModel.content.width;
		}
		/**
		 * @private
		 */
		override protected function onClick(e:MouseEvent):void
		{
			super.onClick(e);
			stage.focus = e_text;
		}
		/**
		 * @private
		 */
		protected function onChange(e:Event):void
		{
			var newHeight:Number = e_text.height;
			if(_lastHeight != newHeight)
			{
				if(newHeight > boxModel.content.height)
				{
					verticalScrollBar.scroll(newHeight - _lastHeight);
				}
				_lastHeight = newHeight;
				updateContentBox();
			}
		}
	}
}